//收藏项的详情
import React, { useEffect, useState } from 'react'
import { FlatList, StyleSheet, TouchableOpacity, View, Image, ImageBackground, ScrollView, Dimensions } from 'react-native';
import { Entypo } from '@expo/vector-icons';
import { AntDesign } from '@expo/vector-icons';
import { Divider } from '@rneui/themed';
import { Text, Card, Button, Icon, ButtonGroup } from '@rneui/themed';
import { getCollectionSingle, getNews, getNewsComment, getNewsCommentImg, getNewsImg } from '../../api/order';

const CollectionInfo = ({ navigation, route }) => {
    const { navigate } = navigation;
    //放置收藏的详情
    const [item, setItem] = useState({});
    //news
    const [newsInfo, setNewsInfo] = useState({});
    //newsimg
    const [newsImg, setNewsImg] = useState({});
    //newscomment
    //判断是否存在comment
    const [newsCommentIs, setNewsCommentIs] = useState('');
    const [newsComment, setNewsComment] = useState({});
    //commentimg
    //判断是否存在commentimg
    const [newsCommentImgIs, setNewsCommentImgIs] = useState('');
    const [newsCommentImg, setNewsCommentImg] = useState({});

    //newstime
    const [newsTime, setNewsTime] = useState('');
    //collect time
    const [collectTime, setCollectTime] = useState('');
    //comment time
    const [commentTime, setCommentTime] = useState('');

    //查询收藏详情
    useEffect(() => {
        getCollectionSingle({ collect_id: route })
            .then(res => {
                console.log(res.data);
                setItem(res.data);
                setCollectTime(new Date(item.collect_time));
            })
    }, [])

    useEffect(() => {
        getNews({ newsid: item.newsid })
            .then(res => {
                setNewsInfo(res.data);
                setNewsTime(new Date(newsInfo.addtime));
            })
    }, [item])

    useEffect(() => {
        getNewsImg({ newsid: item.newsid })
            .then(res => {
                setNewsImg(res.data);
            })
    }, [item])

    useEffect(() => {
        getNewsComment({ newsid: item.newsid })
            .then(res => {
                setNewsComment(res.data);
                setCommentTime(new Date(newsComment.create_time));
                setNewsCommentIs(res.msg);
            })
    }, [item])

    useEffect(() => {
        getNewsCommentImg({ newscom_id: newsComment.newscom_id })
            .then(res => {
                setNewsCommentImgIs(res.msg);
                setNewsCommentImg(res.data);
            })
    }, [newsComment])

    return <ScrollView>
        <View>
            <Card>
                <Card.Title>{item.collect_time}</Card.Title>
                <Card.Divider />
                <Text>用户id：</Text>
                <Text style={{ marginBottom: 10 }}>
                    {newsInfo.userid}
                </Text>
                <Text>发布时间：</Text>
                <Text style={{ marginBottom: 10 }}>
                    {newsTime}
                </Text>
                <Text>帖子内容：</Text>
                <Text style={{ marginBottom: 10 }}>
                    {newsInfo.newstext}
                </Text>
                <Card.Image
                    style={{ padding: 0 }}
                    source={newsImg.newsimgs}
                />
                <Card.Divider />
                <Text>评论：</Text>
                (
                <Text>
                    {
                        (newsCommentIs == '存在newscomment')
                            ?
                            (
                                <Text>
                                    <Text style={{ marginBottom: 10 }}>{newsComment.content}</Text>
                                    (
                                    <View>
                                        {
                                            (newsCommentImgIs == 'newscommentimg')
                                                ?
                                                <Card.Image
                                                    style={{ padding: 0 }}
                                                    source={newsCommentImg.imgs}
                                                />
                                                :
                                                newsCommentImgIs
                                        }
                                    </View>
                                    )
                                    <Text>发表时间：</Text>
                                    <Text style={{ marginBottom: 10 }}>
                                        {collectTime}
                                    </Text>
                                </Text>
                            )
                            : newsCommentIs
                    }
                </Text>
                )
            </Card>
        </View>
    </ScrollView>
}

export default CollectionInfo;